package com.wx.wechat.Scheduled;

import com.wx.wechat.Al.model.ApiModel;
import com.wx.wechat.Al.model.BaiduChatMessage;
import com.wx.wechat.Al.model.ErnieBotTurboResponse;
import com.wx.wechat.Al.model.ErnieBotTurboStreamParam;
import com.wx.wechat.Al.service.BaiduService;
import com.wx.wechat.mail.service.MailService;
import com.wx.wechat.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@Component
@EnableScheduling
@Slf4j
public class LoveScheduled {
    private static final String OPEN_ID = "o8rpM6xztYyLBxrOdd9PTcQmD8WA";
    private static final String OPEN_ID_BY_ME = "gh_e4c7fc8f6884";

    private static final String EMIL_TOPIC= "男友的提醒";
    private static final String EMIL_USER_NAME= "2279854855@qq.com";
    private static final String EMIL_TO_USER_NAME= "1418850095@qq.com";
    @Resource
    private ApiModel apiModel;

    @Resource
    private MailService mailService;
    @Resource
    private RedisUtil redisUtil;
    @PostConstruct
    public void runOnceAtStartup() {
//        // 在这里执行你的任务，只会在应用启动时执行一次
//        redisUtil.setNx("timeKey","10",2l, TimeUnit.HOURS);
//        mailService.sendTextMailMessage(EMIL_TO_USER_NAME,EMIL_TOPIC,"下来听我讲课");
//        log.info("定时任务执行一次");
    }
    // fixedRate参数表示任务执行的固定速率，即每隔两小时执行一次
    @Scheduled(fixedRate = 2 * 60 * 60 * 1000) // 2 hours in milliseconds
    public void sendLoveMessage() {
//        try {
//            String value = redisUtil.get("timeKey");
//            if (StringUtils.isBlank(value)){
//                Integer integerValue = Integer.valueOf(value);
//                integerValue--;
//                redisUtil.decr("timeKey",1L);
//                if (integerValue <0){
//                    log.info("任务结束");
//                    return;
//                }
//                log.info("定时任务执行次数：",integerValue);
//            }
//        } catch (NumberFormatException e) {
//            throw new RuntimeException(e);
//        }
//        BaiduService baiduService = new BaiduService(apiModel.getAPI_KEY(), apiModel.getSECRET_KEY());
//        ErnieBotTurboStreamParam params = new ErnieBotTurboStreamParam();
//        BaiduChatMessage message = new BaiduChatMessage("user","写一首情诗");
//        ArrayList<BaiduChatMessage> baiduChatMessages = new ArrayList<>();
//        baiduChatMessages.add(message);
//        params.setMessages(baiduChatMessages);
//        ErnieBotTurboResponse response = baiduService.ernieBotTurbo(params);
//        String result = response.getResult();
//        log.info("生成情话：" + result);
//        log.info("发送祝福邮件：" + result);
//        String msg = "出门在外注意安全！！！！！";
//        result=msg+"： "+result;
//        log.info(result);
//        mailService.sendTextMailMessage(EMIL_TO_USER_NAME,EMIL_TOPIC,result);
    }
}
